#include "Graphics.h"

void InitGraphics() {
	CONSOLE_HANDLE = GetStdHandle(STD_OUTPUT_HANDLE);
}

void SetColor(WORD color) {
	SetConsoleTextAttribute(CONSOLE_HANDLE,color);
}

void SetWindowSize(DWORD length, DWORD height) {
    COORD NewSBSize;
    SMALL_RECT DisplayArea = {0, 0, 0, 0};

    NewSBSize;
	NewSBSize.X = length;
	NewSBSize.Y = height;

    SetConsoleScreenBufferSize(CONSOLE_HANDLE,NewSBSize);

    DisplayArea.Right = NewSBSize.X - 1;
    DisplayArea.Bottom = NewSBSize.Y - 1;

    SetConsoleWindowInfo(CONSOLE_HANDLE,TRUE,&DisplayArea);
}

void FillChar(TCHAR c, DWORD length, DWORD xPos, DWORD yPos) {
	COORD C;
	C.X = xPos; C.Y = yPos;
	DWORD dump;
	
	FillConsoleOutputCharacter(CONSOLE_HANDLE, c, length, C, &dump);
}

void FillChar2(TCHAR c, DWORD sx, DWORD sy, DWORD ex, DWORD ey) {
	DWORD length = ex - sx;
	DWORD y = ey - sy;
	
	while(y > 0) {
		FillChar(c, length, sx, sy + y);
		y-=1;
	}
	FillChar(c, length, sx, sy + y);
}

void SetPointerPos(DWORD x, DWORD y) {
	COORD pos;
	pos.X = x;
	pos.Y = y;
    SetConsoleCursorPosition(CONSOLE_HANDLE,pos);
}

void DrawChar(TCHAR c, DWORD x, DWORD y) {
	SetPointerPos(x,y);
	printf("%c",c);
}

void DrawBox(DWORD x, DWORD y, DWORD w, DWORD h, WORD color) {
	SetColor(color);
	DrawChar(218,x,y);
	DrawChar(192,x,y+h-1);
	DrawChar(191,x+w-1,y);
	DrawChar(217,x+w-1,y+h-1);
	
	DWORD i;
	for(i = x+1; i < x+w-1; i++) { DrawChar(196,i,y); DrawChar(196,i,y+h-1); }
	for(i = y+1; i < y+h-1; i++) { DrawChar(179,x,i); DrawChar(179,x+w-1,i); }
	FillChar2(' ',x+1,y+1,x+w-1,y+h-2);
}

void DrawHrLine(DWORD y, DWORD length, DWORD color) {
	SetColor(color);
	DWORD i = 0;
	for(i; i < length; i++) DrawChar(205,i,y);
}

